import {
    request
} from "../../request/index"

// pages/goods_list/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        leftCategories: [],
        RightCategories: [],
        currentIndex: 0,
        scrollNum: 0
    },
    categories: [],
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 
        //获取本地存储的数据 
        const Cates = wx.getStorageSync("cates");
        //判断
        if (!Cates) {
            this.getCategories();
        } else {
            if (Date.now() - Cates.time > 1000 * 10) {
                this.getCategories();
            } else {
                console.log("使用旧数据");
                this.categories = Cates.data
                //构造左侧的菜单
                let leftCategories = this.categories.map(v => v.cat_name);
                let RightCategories = this.categories[0].children
                this.setData({
                    leftCategories,
                    RightCategories
                })
            }
        }

    },
    async getCategories() {
        const res = await request({
            url: 'https://api-hmugo-web.itheima.net/api/public/v1/categories'
        });
        this.categories = res.data.message;
        //把接口数据存入本地存储
        wx.setStorageSync('cates', {
            time: Date.now(),
            data: this.categories
        });
        //构造左侧的菜单
        let leftCategories = this.categories.map(v => v.cat_name);
        let RightCategories = this.categories[0].children
        this.setData({
            leftCategories,
            RightCategories,
        })
    },
    changeMenuShow(e) {
        const {
            index
        } = e.currentTarget.dataset;
        let RightCategories = this.categories[index].children
        this.setData({
            currentIndex: index,
            RightCategories,
            scrollNum: 0
        })
    }
})